home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spamexperts / Version.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  5.5 KB  |  179 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Simple version repository for SpamBayes core, and our main apps.
  5.  
  6. Also has the ability to load this version information from a remote location
  7. (in that case, we actually load a "ConfigParser" version of the file to
  8. avoid importing code we can\'t trust.)  This allows any app to check if there
  9. is a later version available.
  10.  
  11. The makefile process for the website will execute this as a script, which
  12. will generate the "ConfigParser" version for the web.
  13. '''
  14. import spamexperts
  15. LATEST_VERSION_HOME = 'http://spambayes.sourceforge.net/download/Version.cfg'
  16. versions = {
  17.     'Version': 0.29999999999999999,
  18.     'Description': 'SpamBayes Engine',
  19.     'Date': 'January 2004',
  20.     'Full Description': '%(Description)s Version %(Version)s (%(Date)s)',
  21.     'Apps': {
  22.         'SpamExperts': {
  23.             'Version': 1.1000000000000001,
  24.             'Product Version': spamexperts.__version__,
  25.             'Description': 'SpamExperts',
  26.             'Date': 'May 2005',
  27.             'Full Description': '%(Description)s v%(Version)s (%(Date)s)' } } }
  28.  
  29. def get_version_string(app = None, description_key = 'Full Description', version_dict = None):
  30.     '''Get a pretty version string, generally just to log or show in a UI'''
  31.     if version_dict is None:
  32.         version_dict = versions
  33.     
  34.     if app is None:
  35.         dict = version_dict
  36.     else:
  37.         dict = version_dict['Apps'][app]
  38.     return dict[description_key] % dict
  39.  
  40.  
  41. def get_version_number(app = None, version_key = 'Version', version_dict = None):
  42.     '''Get a version number, as a float.  This would primarily be used so some
  43.     app or extension can determine if we are later than a specific version
  44.     of either the engine or a specific app.
  45.     Maybe YAGNI.
  46.     '''
  47.     if version_dict is None:
  48.         version_dict = versions
  49.     
  50.     if app is None:
  51.         dict = version_dict
  52.     else:
  53.         dict = version_dict['Apps'][app]
  54.     return dict[version_key]
  55.  
  56.  
  57. try:
  58.     import ConfigParser
  59.     
  60.     class MySafeConfigParser(ConfigParser.SafeConfigParser):
  61.         
  62.         def optionxform(self, optionstr):
  63.             return optionstr
  64.  
  65.  
  66. except AttributeError:
  67.     MySafeConfigParser = None
  68.  
  69.  
  70. def fetch_latest_dict(url = LATEST_VERSION_HOME):
  71.     if MySafeConfigParser is None:
  72.         raise RuntimeError, 'Sorry, but only Python 2.3 can trust remote config files'
  73.     
  74.     import urllib2
  75.     options = options
  76.     import spamexperts.Options
  77.     server = options[('globals', 'proxy_server')]
  78.     if server != '':
  79.         if ':' in server:
  80.             (server, port) = server.split(':', 1)
  81.             port = int(port)
  82.         else:
  83.             port = 8080
  84.         if options[('globals', 'proxy_username')]:
  85.             user_pass_string = '%s:%s' % (options[('globals', 'proxy_username')], options[('globals', 'proxy_password')])
  86.         else:
  87.             user_pass_string = ''
  88.         proxy_support = urllib2.ProxyHandler({
  89.             'http': 'http://%s@%s:%d' % (user_pass_string, server, port) })
  90.         opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
  91.         urllib2.install_opener(opener)
  92.     
  93.     stream = urllib2.urlopen(url)
  94.     cfg = MySafeConfigParser()
  95.     cfg.readfp(stream)
  96.     ret_dict = { }
  97.     apps_dict = ret_dict['Apps'] = { }
  98.     for sect in cfg.sections():
  99.         if sect == 'SpamBayes':
  100.             target_dict = ret_dict
  101.         else:
  102.             target_dict = apps_dict.setdefault(sect, { })
  103.         for opt in cfg.options(sect):
  104.             val = cfg.get(sect, opt)
  105.             
  106.             try:
  107.                 val = float(val)
  108.             except ValueError:
  109.                 pass
  110.  
  111.             target_dict[opt] = val
  112.         
  113.     
  114.     return ret_dict
  115.  
  116.  
  117. def _make_cfg_section(stream, key, this_dict):
  118.     stream.write('[%s]\n' % key)
  119.     for name, val in this_dict.items():
  120.         if type(val) == type(''):
  121.             val_str = repr(val)[1:-1]
  122.         elif type(val) == type(0.0):
  123.             val_str = str(val)
  124.         elif type(val) == type({ }):
  125.             val_str = None
  126.         else:
  127.             print 'Skipping unknown value type: %r' % val
  128.             val_str = None
  129.         if val_str is not None:
  130.             stream.write('%s:%s\n' % (name, val_str))
  131.             continue
  132.     
  133.     stream.write('\n')
  134.  
  135.  
  136. def make_cfg(stream):
  137.     stream.write('# This file is generated from spamexperts/Version.py - do not edit\n')
  138.     _make_cfg_section(stream, 'SpamBayes', versions)
  139.     for appname in versions['Apps']:
  140.         _make_cfg_section(stream, appname, versions['Apps'][appname])
  141.     
  142.  
  143.  
  144. def main(args):
  145.     import sys
  146.     if '-g' in args:
  147.         make_cfg(sys.stdout)
  148.         sys.exit(0)
  149.     
  150.     print 'SpamBayes engine version:', get_version_string()
  151.     print 
  152.     print 'Application versions:'
  153.     for app in versions['Apps']:
  154.         print '\n%s: %s' % (app, get_version_string(app))
  155.     
  156.     print 
  157.     print 'Fetching the lastest version information...'
  158.     
  159.     try:
  160.         latest_dict = fetch_latest_dict()
  161.     except:
  162.         print 'FAILED to fetch the latest version'
  163.         import traceback
  164.         traceback.print_exc()
  165.         sys.exit(1)
  166.  
  167.     print 
  168.     print 'SpamBayes engine version:', get_version_string(version_dict = latest_dict)
  169.     print 
  170.     print 'Application versions:'
  171.     for app in latest_dict['Apps']:
  172.         print '\n%s: %s' % (app, get_version_string(app, version_dict = latest_dict))
  173.     
  174.  
  175. if __name__ == '__main__':
  176.     import sys
  177.     main(sys.argv)
  178.  
  179.